package com.cz.nongyetongv5.adapter;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.activity.TongXunLuActivity;
import com.cz.nongyetongv5.adapter.config.CommonConstants;
import com.cz.nongyetongv5.adapter.dao.DatabaseHelper;
import com.cz.nongyetongv5.bean.LinkMan;
import com.cz.nongyetongv5.bean.Town;

public class TongXunLuListAdapter extends BaseAdapter {

	// 上下文对象
	private Context context = null;

	private List datas = null;

	private DatabaseHelper db = null;

	private TongXunLuActivity onClickListener = null;

	public TongXunLuListAdapter(Context context, List datas, DatabaseHelper db,
								TongXunLuActivity onClickListener) {
		this.context = context;
		this.datas = datas;
		this.db = db;
		this.onClickListener = onClickListener;
	}

	@Override
	public int getCount() {
		return datas == null ? 0 : datas.size();
	}

	@Override
	public Object getItem(int arg0) {
		return datas.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {

		ViewGroup tongXunLuItemLayout = null;

		if (view == null) {
			tongXunLuItemLayout = (ViewGroup) LayoutInflater.from(context)
					.inflate(R.layout.tongxunlu_item_layout, null);
		} else {
			tongXunLuItemLayout = (ViewGroup) view;
		}

		ViewGroup llTongXuLuRightLayout = (ViewGroup) tongXunLuItemLayout
				.findViewById(R.id.llTongXuLuRightLayout);

		View ivCall = tongXunLuItemLayout.findViewById(R.id.ivCall);

		View ivMore = tongXunLuItemLayout.findViewById(R.id.ivMore);

		TextView tvTitle = (TextView) tongXunLuItemLayout
				.findViewById(R.id.tvTitle);

		Object data = datas.get(position);

		if (data != null && data instanceof LinkMan) {

			llTongXuLuRightLayout.setClickable(true);
			ivCall.setVisibility(View.VISIBLE);
			ivMore.setVisibility(View.GONE);
			llTongXuLuRightLayout.setTag(position);

			LinkMan linkMain = (LinkMan) data;

			tvTitle.setText(linkMain.getRealName());

			llTongXuLuRightLayout.setOnClickListener(onClickListener);

		}

		if (data != null && data instanceof Town) {

			llTongXuLuRightLayout.setClickable(false);

			ivCall.setVisibility(View.GONE);
			ivMore.setVisibility(View.VISIBLE);

			Town town = (Town) data;

			int count = db.queryLinkManCountByTownId(town.getCode(),
					onClickListener.getCurrentCategory());


			tvTitle.setText(town.getName() + "\u0020[" + count + "]");

		}

		return tongXunLuItemLayout;
	}

}
